/*
 * **.cpp - 
 *
 * Copyright (C) 2010-2011 Serge Begger
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "powerconf.h"
#include "ui_powerconf.h"
#include <math.h>

powerconf::powerconf(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::powerconf)
{
    ui->setupUi(this);
    netpowerDevice = new netpower(this);
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(endTimer()));
    timer->start(400);
    firstRead = true;
    ui->centralWidget->setDisabled(true);
}

bool powerconf::deviceEnabled() {
    return netpowerDevice->deviceOpen;
}

void powerconf::endTimer() {
    if (netpowerDevice->deviceOpen) {
        ui->centralWidget->setDisabled(false);
        netpowerDevice->readData();
        double inV = (((netpowerDevice->netpowerData[AKB_VOLTAGE_H]<<8) + netpowerDevice->netpowerData[AKB_VOLTAGE]) * 28) / 1000.0;
        ui->label_VoltageIn->setText(QString::number(floor(inV*10)/10,'f', 1));
        double outV = (((netpowerDevice->netpowerData[OUT_VOLTAGE_H]<<8) + netpowerDevice->netpowerData[OUT_VOLTAGE]) * 28) / 1000.0;
        ui->label_VoltageOut->setText(QString::number(floor(outV*10)/10,'f', 1));

        if (firstRead) {
            ui->labelACCON->setText(QString::number(netpowerDevice->netpowerData[ACC_ON_DELAY]));
            ui->labelPOWON->setText(QString::number(netpowerDevice->netpowerData[PC_ON_DELAY]));
            ui->labelROUT->setText(QString::number(netpowerDevice->netpowerData[R_OUT_DELAY]));
            ui->labelBATOFF->setText(QString::number(netpowerDevice->netpowerData[AKB_OFF_DELAY]));
            ui->labelACCOFF->setText(QString::number((netpowerDevice->netpowerData[ACC_OFF_DELAY_H]<<8) + netpowerDevice->netpowerData[ACC_OFF_DELAY]));
            ui->labelOSSHDWNTIME->setText(QString::number(netpowerDevice->netpowerData[OS_SHUTDOWN_TIME]));

            ui->labelBATMIN->setText(QString::number((double)netpowerDevice->netpowerData[AKB_MIN] / 10 ,'f', 1));
            ui->labelBATMAX->setText(QString::number((double)netpowerDevice->netpowerData[ON_AKB_MIN] / 10 ,'f', 1));

            setCheckPCStatus((bool)netpowerDevice->netpowerData[CHECK_PC_ON]);
            setSleepStatus((bool)netpowerDevice->netpowerData[SLEEP_MODE]);
            firstRead = false;
        }
    } else {
        ui->centralWidget->setDisabled(true);
        netpowerDevice->deviceOpen = netpowerDevice->init();
    }
    ui->labelStatus->setText("Device status: "+netpowerDevice->deviceStatus);
}

void powerconf::rereadData() {
    if (!netpowerDevice->deviceOpen)
        return;

    ui->centralWidget->setDisabled(true);
    firstRead = true;
}

void powerconf::writeData() {
    if (!netpowerDevice->deviceOpen)
        return;

    ui->centralWidget->setDisabled(true);
    timer->stop();
    firstRead = true;

    netpowerDevice->netpowerData[ACC_ON_DELAY] = ui->labelACCON->text().toInt();
    netpowerDevice->netpowerData[PC_ON_DELAY] = ui->labelPOWON->text().toInt();
    netpowerDevice->netpowerData[R_OUT_DELAY] = ui->labelROUT->text().toInt();
    netpowerDevice->netpowerData[AKB_OFF_DELAY] = ui->labelBATOFF->text().toInt();

    netpowerDevice->netpowerData[ACC_OFF_DELAY_H] = ui->labelACCOFF->text().toInt() >> 8;
    netpowerDevice->netpowerData[ACC_OFF_DELAY] = ui->labelACCOFF->text().toInt();

    netpowerDevice->netpowerData[OS_SHUTDOWN_TIME] = ui->labelOSSHDWNTIME->text().toInt();

    netpowerDevice->netpowerData[AKB_MIN] = (int)(ui->labelBATMIN->text().toDouble() * 10);
    netpowerDevice->netpowerData[ON_AKB_MIN] = (int)(ui->labelBATMAX->text().toDouble() * 10);

    netpowerDevice->netpowerData[CHECK_PC_ON] = ui->pb_CHECKPC->isChecked() ? 1 : 0;
    netpowerDevice->netpowerData[SLEEP_MODE] = ui->pb_SLEEP->isChecked() ? 1 : 0;
    netpowerDevice->writeData();

    timer->start(400);
}


powerconf::~powerconf()
{
    delete ui;
}

void powerconf::on_pb_CHECKPC_clicked(bool state)
{
    setCheckPCStatus(state);
}

void powerconf::setCheckPCStatus(bool state)
{
    if (state)
        ui->pb_CHECKPC->setText("ON");
    else
        ui->pb_CHECKPC->setText("OFF");
}

void powerconf::on_pb_SLEEP_clicked(bool state)
{
    setSleepStatus(state);
}

void powerconf::setSleepStatus(bool state)
{
    if (state)
        ui->pb_SLEEP->setText("ON");
    else
        ui->pb_SLEEP->setText("OFF");
}

void powerconf::on_pb_ACCON_Up_pressed() {
    ui->labelACCON->setText(up(ui->labelACCON->text().toInt()));
}

void powerconf::on_pb_ACCON_Dwn_pressed() {
    ui->labelACCON->setText(down(ui->labelACCON->text().toInt()));
}

void powerconf::on_pb_POWON_Up_pressed() {
    ui->labelPOWON->setText(up(ui->labelPOWON->text().toInt()));
}

void powerconf::on_pb_POWON_Dwn_pressed() {
    ui->labelPOWON->setText(down(ui->labelPOWON->text().toInt()));
}

void powerconf::on_pb_ROUT_Up_pressed() {
    ui->labelROUT->setText(up(ui->labelROUT->text().toInt()));
}

void powerconf::on_pb_ROUT_Dwn_pressed() {
    ui->labelROUT->setText(down(ui->labelROUT->text().toInt()));
}

void powerconf::on_pb_BATTOFF_Up_pressed() {
    ui->labelBATOFF->setText(up(ui->labelBATOFF->text().toInt()));
}

void powerconf::on_pb_BATTOFF_Dwn_pressed() {
    ui->labelBATOFF->setText(down(ui->labelBATOFF->text().toInt()));
}

void powerconf::on_pb_ACCOFF_Up_pressed() {
    ui->labelACCOFF->setText(up(ui->labelACCOFF->text().toInt(),65535));
}

void powerconf::on_pb_ACCOFF_Dwn_pressed() {
    ui->labelACCOFF->setText(down(ui->labelACCOFF->text().toInt()));
}

void powerconf::on_pb_OSSHDWNTIME_up_pressed() {
    ui->labelOSSHDWNTIME->setText(up(ui->labelOSSHDWNTIME->text().toInt()));
}

void powerconf::on_pb_OSSHDWNTIME_Dwn_pressed() {
    ui->labelOSSHDWNTIME->setText(down(ui->labelOSSHDWNTIME->text().toInt()));
}

void powerconf::on_pb_BATMIN_up_pressed() {
    ui->labelBATMIN->setText(upVolt(ui->labelBATMIN->text().toDouble()));
}

void powerconf::on_pb_BATMIN_Dwn_pressed() {
    ui->labelBATMIN->setText(downVolt(ui->labelBATMIN->text().toDouble()));
}

void powerconf::on_pb_BATMAX_up_pressed() {
    ui->labelBATMAX->setText(upVolt(ui->labelBATMAX->text().toDouble()));
}

void powerconf::on_pb_BATMAX_Dwn_pressed() {
    ui->labelBATMAX->setText(downVolt(ui->labelBATMAX->text().toDouble()));
}

QString powerconf::upVolt(double inc)
{
    inc = inc + 0.1;
    if (inc > 12.0)
        inc = 12.0;
    return QString::number(inc,'f', 1);
}

QString powerconf::downVolt(double dec)
{
    dec = dec - 0.1;
    if (dec < 9.0)
        dec = 9.0;
    return QString::number(dec,'f', 1);
}

QString powerconf::up(int inc,int max)
{
    inc++;
    if (inc > max)
        inc = max;
    return QString::number(inc);
}

QString powerconf::down(int data)
{
    data--;
    if (data < 0)
        data = 0;
    return QString::number(data);
}
